package com.huiquan.vocab.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import com.huiquan.framework.base.EnhancedBaseDao;
import com.huiquan.vocab.constant.VocabConstant;
import com.huiquan.vocab.domain.VocabDyadStd;
import com.ibatis.sqlmap.client.SqlMapClient;

@Repository
public class VocabDyadStdDao extends EnhancedBaseDao<VocabDyadStd> {

	@Resource(name = "sqlMapClient")
	private SqlMapClient sqlMapClient;

	public static final String space = "VOCAB_DYAD_STD.";

	public VocabDyadStdDao() {
		super(space);
	}

	public int retrieveSizeByOs(Map<String, Object> param) {
		Integer size = (Integer) this.object(space + "RETRIEVE_SIZE_BY_O_S", param, sqlMapClient);
		return size == null ? 0 : size;
	}

	public void updateStdFlagByIds(String ids, Integer flag, Long uid, Long checkId, Integer synonymFlag) {
		Map<String, Object> param = new HashMap<>();
		param.put("ids", ids);
		param.put("flag", flag);
		param.put("uid", uid);
		param.put("checkId", checkId);
		param.put("synonymFlag", synonymFlag);

		this.update(space + "UPDATE_FLAG_BY_IDS", param, sqlMapClient);
	}

	public void updateDyadStd(String id, String array, String partPattern, Long uid) {
		Map<String, Object> param = new HashMap<>();
		param.put("id", id);
		param.put("array", array);
		param.put("partPattern", partPattern);
		param.put("uid", uid);
		param.put("combineS", getCombineS(partPattern));

		this.update(space + "UPDATE_STD_BY_DYAD", param, sqlMapClient);
	}

	public Long insertStd(String type, String array, String partPattern, Long uid) {
		Map<String, Object> param = new HashMap<>();
		param.put("type", type);
		param.put("array", array);
		param.put("partPattern", partPattern);
		param.put("uid", uid);
		String combineS = "";
		if (VocabConstant.DYAD_O_O.equals(type)) {
			combineS = getCombineS(partPattern);
		}
		param.put("combineS", combineS);

		return (Long) this.insert(space + "INSERT_STD", param, sqlMapClient);
	}

	private String getCombineS(String partPattern) {
		String combineS = "";
		String[] ps = partPattern.substring(1).split(",");
		for (int i = 0; i < ps.length; i++) {
			if (i == ps.length - 1) {
				combineS += "1,";
			} else {
				combineS += "0,";
			}
		}
		if (!combineS.isEmpty()) {
			combineS = "," + combineS;
		}

		return combineS;
	}

	public void deleteNoSynonym() {
		this.delete(space + "DELETE_NO_SYNONYM", sqlMapClient);
	}

	public boolean isNewOrderVocabulary(String id) {
		Integer count = (Integer) this.object(space + "RETRIEVE_SIZE_OF_ORDER_VOCABULARY_IN_PAGE", id, sqlMapClient);
		count = count == null ? 0 : count;
		return count > 0 ? false : true;
	}

	public void updatePageByStdId(String id) {
		this.update(space + "UPDATE_PAGE_BY_STD_ID", id, sqlMapClient);
	}

	public int retrieveTotalPageNo(String type) {
		Integer size = (Integer) this.object(space + "RETRIEVE_TOTAL_PAGE_NO", type, sqlMapClient);
		return size == null ? 0 : size;
	}

	public String retrievePageOrderVocabulary(Map<String, Object> param) {
		return (String) this.object(space + "RETRIEVE_PAGE_ORDER_VOCABULARY", param, sqlMapClient);
	}

	public void checkDyadBySubWrongIds(String wrongIds, String types) {
		Map<String, Object> param = new HashMap<>();
		param.put("wrongIds", wrongIds);
		param.put("types", types);

		this.update(space + "CHECK_DYAD_BY_SUB_WRONG_IDS", param, sqlMapClient);
	}

	public void resetPOFlagByPP(String checkedIds, Long userId) {
		Map<String, Object> param = new HashMap<>();
		param.put("ids", checkedIds);
		param.put("userId", userId);

		this.update(space + "RESET_P_O_FLAG_BY_P_P", param, sqlMapClient);
	}

	/**
	 * 根据一个Map自定义获取列表
	 * 
	 * @param param
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<VocabDyadStd> retrieveListByOS(Map<String, Object> param) {
		return (List<VocabDyadStd>) this.list(space + "RETRIEVE_LIST_BY_O_S", param, sqlMapClient);
	}

	public void insertMultiOCombineS(String resultArray, String resultStdArray, String resultCombineS) {

		Map<String, Object> param = new HashMap<>();
		param.put("array", resultArray);
		param.put("stdArray", resultStdArray);
		param.put("combineS", resultCombineS);

		this.insert(space + "INSERT_MULTI_O_COMBINE_S", param, sqlMapClient);
	}

	public void updateCombineSByMultiO(String resultStdArray, String resultCombineS) {

		Map<String, Object> param = new HashMap<>();
		param.put("stdArray", resultStdArray);
		param.put("combineS", resultCombineS);

		this.insert(space + "UPDATE_COMBINE_S_BY_MULTI_O", param, sqlMapClient);
	}

	@SuppressWarnings("unchecked")
	public List<String> retrieveOSRepeat() {
		return (List<String>) this.list(space + "RETRIEVE_O_S_REPEAT", sqlMapClient);
	}

	public void deleteById(String id) {
		this.delete(space + "DELETE_BY_ID", id, sqlMapClient);
	}

	public Long insertByObject(VocabDyadStd entity) {
		return (Long) insert(space + "INSERT_BY_OBJECT", entity, sqlMapClient);
	}

	public void initStdArray() {
		this.update(space + "INIT_STD_ARRAY", sqlMapClient);
	}


	@SuppressWarnings("unchecked")
	public List<VocabDyadStd> retrieveInternList(Map<String, Object> param) {
		return (List<VocabDyadStd>) this.list(space + "RETRIEVE_INTERN_LIST", param, sqlMapClient);
	}

	@SuppressWarnings("unchecked")
	public List<VocabDyadStd> retrieveInternTestList(Map<String, Object> param) {
		return (List<VocabDyadStd>) this.list(space + "RETRIEVE_INTERN_TEST_LIST", param, sqlMapClient);
	}

	public synchronized void initInternTest(Map<String, Object> param) {
		this.insert(space + "INIT_INTERN_TEST", param, sqlMapClient);
	}

	public void updateStdFlagByIdsTest(String ids, Integer flag, Long uid) {
		Map<String, Object> param = new HashMap<>();
		param.put("ids", ids);
		param.put("flag", flag);
		param.put("uid", uid);

		this.update(space + "UPDATE_FLAG_BY_IDS_TEST", param, sqlMapClient);
	}

	public Double retrieveTestRightPercent(Map<String, Object> param) {

		Double result = (Double) this.object(space + "RETRIEVE_TEST_RIGHT_PERCENT", param, sqlMapClient);
		return result == null ? 0 : result;
	}

	public void updateDyadStdTest(String id, String array, String partPattern, Long uid) {
		Map<String, Object> param = new HashMap<>();
		param.put("id", id);
		param.put("array", array);
		param.put("partPattern", partPattern);
		param.put("uid", uid);

		this.update(space + "UPDATE_STD_BY_DYAD_TEST", param, sqlMapClient);
	}

	public void updateTest(VocabDyadStd dyad) {

		this.update(space + "UPDATE_TEST", dyad, sqlMapClient);
	}

	public int retrieveInternTestSize(Map<String, Object> param) {
		Integer size = (Integer) this.object(space + "RETRIEVE_INTERN_TEST_SIZE", param, sqlMapClient);
		return size == null ? 0 : size;
	}

	public void bakData(Map<String, Object> param) {
		this.insert(space + "BAK_DATA", param, sqlMapClient);
	}

	public void clearData(Map<String, Object> param) {
		this.delete(space + "CLEAR_DATA", param, sqlMapClient);
	}

	public void resetData(Map<String, Object> param) {
		this.update(space + "UPDATE_DATA", param, sqlMapClient);
	}

	public void mergeSynonymByRight(String ids) {
		Map<String, Object> param = new HashMap<>();
		param.put("ids", ids);
		
		this.update(space + "MERGE_SYNONYM_BY_RIGHT", param, sqlMapClient);
	}
}
